﻿/// <reference path="../libs/_references.js" />
/// <reference path="httpRequester.js" />

window.persisters = (function () {

    var username = localStorage.getItem("username");
    var accessToken = localStorage.getItem("accessToken");

    function saveUserData(userData) {
        localStorage.setItem("username", userData.username);
        localStorage.setItem("accessToken", userData.accessToken);
        username = userData.username;
        accessToken = userData.accessToken;
    }

    function clearUserData() {
        localStorage.removeItem("username");
        localStorage.removeItem("accessToken");
        username = "";
        accessToken = "";
    }

    var UsersPersister = Class.create({
        init: function (apiUrl) {
            this.apiUrl = apiUrl;
        },

        login: function (bashUsername, password) {
            var user = {
                username: bashUsername,
                authCode: CryptoJS.SHA1(password).toString()
            };

            return httpRequester.postJSON(this.apiUrl + "auth/token", user)
                .then(function (data) {
                    saveUserData(data);
                    return data;
                });
        },

        register: function (bashUsername, password, email) {
            var user = {
                username: bashUsername,
                authCode: CryptoJS.SHA1(password).toString(),
                email: email
            };

            return httpRequester.postJSON(this.apiUrl + "users/register", user)
                .then(function (data) {
                    saveUserData(data);
                    return data.username;
                });
        },

        logout: function () {
            if (!accessToken) {
                alert("You are not logged in!");
            } else {
                var headers = {
                    "X-accessToken": accessToken
                };

                return httpRequester.putJSON(this.apiUrl + "users", "", headers)
                    .then(function (response) {
                        clearUserData();
                        return response;
                    });
            }
        }
    });

    var MainPersister = Class.create({
        init: function (apiUrl) {
            this.users = new UsersPersister(apiUrl);
            this.todos = new TodosPerister(apiUrl);
            this.appointments = new AppointmentsPersister(apiUrl);
        },

        isUserLoggedIn: function () {
            var isLoggedIn = username != null && accessToken != null;
            return isLoggedIn;
        },

        getUsername: function () {
            return username;
        }
    });

    var TodosPerister = Class.create({
        init: function (apiUrl) {
            this.apiUrl = apiUrl + "lists";
        },

        create: function (title, todos) {
            if (!accessToken) {
                //TODO: Tell him/her to login
            } else {
                var appData = {
                    title: title,
                    todos: todos
                };

                var headers = {
                    "X-accessToken": accessToken
                };

                return httpRequester.postJSON(this.apiUrl, appData, headers)
                    .then(function (data) {
                        return data;
                    });
            }
        },

        createSingleTodo: function (listId, text) {
            if (!accessToken) {
                //TODO: Tell him/her to login
            } else {
                var appData = {
                    text: text
                };

                var headers = {
                    "X-accessToken": accessToken
                };

                return httpRequester.postJSON(this.apiUrl + "/" + listId + "/todos", appData, headers)
                    .then(function (data) {
                        return data;
                    });
            }
        },

        all: function () {
            if (!accessToken) {
                //TODO: Tell him/her to login
            } else {
                var headers = {
                    "X-accessToken": accessToken
                };

                return httpRequester.getJSON(this.apiUrl, headers);
            }
        },

        getById: function (id) {
            if (!accessToken) {
                //TODO: Tell him/her to login
            } else {
                var headers = {
                    "X-accessToken": accessToken
                };

                return httpRequester.getJSON(this.apiUrl + "/" + id + "/todos", headers);
            }
        }
    });

    var AppointmentsPersister = Class.create({
        init: function (apiUrl) {
            this.apiUrl = apiUrl + "appointments";
        },

        create: function (subject, description, appointmentDate, duration) {
            if (!accessToken) {
                //TODO: Tell him/her to login
            } else {
                var appData = {
                    subject: subject,
                    description: description,
                    appointmentDate: appointmentDate,
                    duration: duration
                };

                var headers = {
                    "X-accessToken": accessToken
                };

                return httpRequester.postJSON(this.apiUrl, appData, headers)
                    .then(function (data) {
                        return data;
                    });
            }
        },

        all: function () {
            if (!accessToken) {
                //TODO: Tell him/her to login
            } else {
                var headers = {
                    "X-accessToken": accessToken
                };

                return httpRequester.getJSON(this.apiUrl + "/all", headers);
            }
        },

        getAllComming: function () {
            if (!accessToken) {
                //TODO: Tell him/her to login
            } else {
                var headers = {
                    "X-accessToken": accessToken
                };

                return httpRequester.getJSON(this.apiUrl + "/comming", headers);
            }
        },

        getTodays: function () {
            if (!accessToken) {
                //TODO: Tell him/her to login
            } else {
                var headers = {
                    "X-accessToken": accessToken
                };

                return httpRequester.getJSON(this.apiUrl + "/today", headers);
            }
        },

        getCurrent: function () {
            if (!accessToken) {
                alert("You are not logged in!");
            } else {
                var headers = {
                    "X-accessToken": accessToken
                };

                return httpRequester.getJSON(this.apiUrl + "/current", headers);
            }
        },

        getByDate: function (date) {
            if (!accessToken) {
                //TODO: Tell him/her to login
            } else {
                var headers = {
                    "X-accessToken": accessToken
                };

                return httpRequester.getJSON(this.apiUrl + "?date=" + date, headers);
            }
        }
    });

    return {
        getPersister: function (apiUrl) {
            return new MainPersister(apiUrl);
        }
    };

}());